
package io.zhifei.modules.sys.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.zhifei.common.annotation.SysLog;
import io.zhifei.common.utils.Constant;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.R;
import io.zhifei.common.validator.ValidatorUtils;
import io.zhifei.modules.sys.system.entity.SysRoleEntity;
import io.zhifei.modules.sys.system.service.SysRoleMenuService;
import io.zhifei.modules.sys.system.service.SysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/role")
@Api(description = "角色管理")
public class SysRoleController extends AbstractController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Value("${role.roleList}")
    private String roleList;

    /**
     * 角色列表
     */
    @GetMapping("/list")
//    @RequiresPermissions("sys:role:list")
    @ApiOperation("角色列表")
    public R list(@RequestParam Map<String, Object> params) {
        //如果不是超级管理员，则只查询自己创建的角色列表
        if (getUserId() != Constant.SUPER_ADMIN) {
            params.put("createUserId", getUserId());
        }

        PageUtils page = sysRoleService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 角色列表
     */
    @GetMapping("/select")
//    @RequiresPermissions("sys:role:select")
    @ApiOperation("角色列表")
    public R select() {
        Map<String, Object> map = new HashMap<>();

       /* //如果不是超级管理员，则只查询自己所拥有的角色列表
        if (getUserId() != Constant.SUPER_ADMIN) {
            map.put("create_user_id", getUserId());
        }*/
        List<SysRoleEntity> list = (List<SysRoleEntity>) sysRoleService.listByMap(map);

        return R.ok().put("list", list);
    }

    /**
     * 角色信息
     */
    @GetMapping("/info/{roleId}")
//    @RequiresPermissions("sys:role:info")
    @ApiOperation("角色信息")
    public R info(@PathVariable("roleId") Long roleId) {
        SysRoleEntity role = sysRoleService.getById(roleId);

        //查询角色对应的菜单
        List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
        role.setMenuIdList(menuIdList);

        return R.ok().put("role", role);
    }

    /**
     * 保存角色
     */
    @SysLog("保存角色")
    @PostMapping("/save")
//    @RequiresPermissions("sys:role:save")
    @ApiOperation("保存角色")
    public R save(@RequestBody SysRoleEntity role) {
        ValidatorUtils.validateEntity(role);

        role.setCreateUserId(getUserId());
        sysRoleService.saveRole(role);

        return R.ok();
    }

    /**
     * 修改角色
     */
    @SysLog("修改角色")
    @PostMapping("/update")
//    @RequiresPermissions("sys:role:update")
    @ApiOperation("修改角色")
    public R update(@RequestBody SysRoleEntity role) {

        role.setCreateUserId(getUserId());
        sysRoleService.update(role);

        return R.ok();
    }

    /**
     * 删除角色
     */
    @SysLog("删除角色")
    @PostMapping("/delete")
//    @RequiresPermissions("sys:role:delete")
    @ApiOperation("删除角色")
    public R delete(@RequestBody Long[] roleIds) {
        //分割不能删除的权限
        String[] roleLists = roleList.split(",");
        for (int i = 1; i <= roleIds.length; i++) {
            for (int j = 0; j < roleLists.length; j++) {
                if (roleIds[i - 1] == Long.valueOf(roleLists[j])) {
                    return R.error("第" + i + "个不能删除");
                }
            }
        }
        sysRoleService.deleteBatch(roleIds);
        return R.ok();
    }
}
